package com.ytjj.qmyx.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.ytjj.common.api.CommonPage;
import com.ytjj.qmyx.admin.eunm.RoleEnums;
import com.ytjj.qmyx.admin.mapper.AdminMapper;
import com.ytjj.qmyx.admin.mapper.OrderProblemMessageMapper;
import com.ytjj.qmyx.admin.model.Admin;
import com.ytjj.qmyx.admin.model.OrderProblemMessage;
import com.ytjj.qmyx.admin.model.request.OrderProblemListRequest;
import com.ytjj.qmyx.admin.model.response.OrderInteractRecords;
import com.ytjj.qmyx.admin.model.response.OrderProblemListResponse;
import com.ytjj.qmyx.admin.service.OrderProblemMessageService;
import com.ytjj.qmyx.admin.service.OrderProblemService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@Slf4j
public class OrderProblemMessageServiceImpl implements OrderProblemMessageService {
    @Resource
    private OrderProblemMessageMapper orderProblemMessageMapper;

    @Resource
    private OrderProblemService orderProblemService;

    @Override
    public CommonPage<OrderProblemMessage> queryMessage(Admin receiveUser, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<OrderProblemMessage>  messageList = orderProblemMessageMapper.queryMessage(receiveUser.getId());
        if (!CollectionUtils.isEmpty(messageList)) {
            messageList  =  isShowList(orderProblemService,receiveUser,messageList);
        }
        return CommonPage.restPage(messageList);
    }

    private static List<OrderProblemMessage> isShowList(OrderProblemService orderProblemService,Admin receiveUser,List<OrderProblemMessage> messageList){
        List<Integer> removeList = new ArrayList<>();
        if (RoleEnums.isCustomer(receiveUser.getRoleCode())) {
            Set<Integer> collect = messageList.stream().map(OrderProblemMessage::getProblemId).collect(Collectors.toSet());
            OrderProblemListRequest request = new OrderProblemListRequest();
            request.setIdList(new ArrayList<>(collect));
            CommonPage<OrderProblemListResponse> page = orderProblemService.orderProblemList(request);
            if (!CollectionUtils.isEmpty(page.getList())) {
                if (!CollectionUtils.isEmpty(page.getList())) {
                    List<OrderInteractRecords> recordsList = page.getList().get(0).getRecordsList();
                    if (!CollectionUtils.isEmpty(recordsList)) {
                        List<OrderInteractRecords> collect2 = recordsList.stream().sorted(Comparator.comparing(OrderInteractRecords::getId).reversed()).collect(Collectors.toList());
                        if (collect2.get(0).getReplyRole().equals(1)) {
                            removeList.add(collect2.get(0).getProblemId());
                        }
                    }
                }
            }
        }
        messageList = messageList.stream().filter(item -> !removeList.contains(item.getProblemId())).collect(Collectors.toList());
        return messageList;
    }

    @Override
    public OrderProblemMessage findById(Integer id) {
        OrderProblemMessage message = orderProblemMessageMapper.selectByPrimaryKey(id);
        message.setStatus(1);
        orderProblemMessageMapper.updateByPrimaryKeySelective(message);
        return message;
    }
}
